<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html>
<html>
<head>
    <jsp:include page="../inc/js.jsp"/>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <title>参数管理</title>
    <jsp:include page="../inc/css.jsp"/>

    <style>
        input[type=checkbox], input[type=radio] {
            transform: translateY(3px);
        }
    </style>
</head>
<body class="hold-transition sidebar-mini">
<div class="wrapper">
    <jsp:include page="../inc/menu.jsp"/>

    <!-- Content Wrapper. Contains page content -->
    <div class="content-wrapper">
        <!-- Main content -->
        <!-- Main content -->
        <section class="content">
            <div class="row">
                <div class="col-12">
                    <div class="card" style="margin-top: 10px">
                        <!-- /.card-header -->
                        <div class="card-header">
                            <button type="button" title="新增参数" class="btn btn-success" onclick="_codeDictList.fn.addInfo()">新增参数</button>

                            <c:if test="${sessionScope.is_localhost}">
                                <button type="button" title="同步原始数据" class="btn btn-info" onclick="_codeDictList.fn.syncData()"> 同步原始数据</button>
                            </c:if>

                            <c:if test="${sessionScope.session_master_id == 1}">
                                <button type="button" title="批量修改参数单位" class="btn btn-primary" onclick="_codeDictList.fn.batchUpdateModal()"> 批量修改参数单位</button>
                            </c:if>

                            <div class="form-inline" style="margin-top: 10px;">
                                <div class="form-group">
                                    <select id="searchTypeList" class="form-control" style="margin-right: 10px;"></select>
                                </div>
                                <div class="form-group">
                                    <input id="searchKeyWord" placeholder="名称或编号" class="form-control" style="margin-right: 10px;"/>
                                </div>
                                <button type="button" title="查询" class="btn btn-info" onclick="_codeDictList.fn.reloadData()"> 查询</button>
                                <button type="button" title="重置搜索条件并查询" class="btn btn-success" style="margin-left: 10px;" onclick="_codeDictList.fn.resetAndReloadData()"> 重置</button>
                                <button type="button" title="批量删除" class="btn btn-danger" style="margin-left: 10px;" onclick="_codeDictList.fn.batchDelModal()"> 批量删除</button>
                            </div>
                        </div>
                        <div class="card-body">
                            <div class="table-responsive">
                                <table class="table table-striped table-bordered table-hover" id="dataTables" style="width: 99%">
                                    <colgroup>
                                        <col class="gradeA even" style="width: 3%"/>
                                        <col class="gradeA odd"/>
                                        <col class="gradeA even"/>
                                        <col class="gradeA odd"/>
                                        <col class="gradeA even"/>
                                        <col class="gradeA odd"/>
                                    </colgroup>
                                    <thead>
                                    <tr>
                                        <th><input type="checkbox" onclick="$tutu.checkAll(this)" class="checkall"/></th>
                                        <th style="min-width: 80px;">协议类型</th>
                                        <th style="min-width: 130px;">参数代码</th>
                                        <th style="min-width: 160px;">参数名称</th>
                                        <th style="min-width: 160px;">参数单位</th>
                                        <th style="width: 80px">操作</th>
                                    </tr>
                                    </thead>
                                    <tbody>
                                    </tbody>
                                </table>
                            </div>
                        </div>
                        <!-- /.card-body -->
                    </div>
                    <!-- /.card -->
                </div>
                <!-- /.col -->
            </div>
            <!-- /.row -->
        </section>
        <!-- /.content -->
    </div>
</div>
<!-- ./wrapper -->

<div class="modal inModal fade" id="codeDictModal" tabindex="-1" role="dialog" aria-hidden="true">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <h4 class="modal-title">参数详情</h4>
                <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
            </div>
            <div class="modal-body">
                <form role="form" class="form-group">
                    <input type="hidden" id="codeDictId"/>

                    <div class="form-inline" style="margin-bottom: 10px;">
                        <label class="col-3">协议类型：</label>

                        <div class="col-9">
                            <select id="typeList" class="form-control" style="width: 95%"></select>
                        </div>
                    </div>

                    <div class="form-inline" style="margin-bottom: 10px;">
                        <label class="col-3">参数代码：</label>

                        <div class="col-9">
                            <input type="text" id="codeDictCode" maxlength="40" class="form-control" style="width: 95%"/>
                        </div>
                    </div>

                    <div class="form-inline" style="margin-bottom: 10px;">
                        <label class="col-3">参数名称：</label>

                        <div class="col-9">
                            <input type="text" id="codeDictName" maxlength="30" class="form-control" style="width: 95%"/>
                        </div>
                    </div>

                    <div class="form-inline" style="margin-bottom: 10px;">
                        <label class="col-3">参数单位：</label>

                        <div class="col-9">
                            <input type="text" id="codeDictUnit" maxlength="40" class="form-control" style="width: 95%"/>
                        </div>
                    </div>
                </form>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-info" onclick="_codeDictList.fn.saveInfoPlus()">保存并添加</button>
                <button type="button" class="btn btn-primary" onclick="_codeDictList.fn.saveInfo(0)">保存</button>
                <button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>
            </div>
        </div>
    </div>
</div>

<div class="modal fade" id="delModal" delModal="-1" role="dialog" aria-labelledby="pwdModalLabel" aria-hidden="true">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header" style="display: inline">
                <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
                <h4 class="modal-title">删除参数警告</h4>
            </div>
            <div class="modal-body" style="word-break: break-all;">
                确定要删除以下参数？
                <br/><span id="delSpan"></span>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>
                <button type="button" class="btn btn-primary" onclick="_codeDictList.fn.batchDel()">确定</button>
            </div>
        </div>
        <!-- /.modal-content -->
    </div>
    <!-- /.modal-dialog -->
</div>

<div class="modal fade" id="updateModal" delModal="-1" role="dialog" aria-labelledby="pwdModalLabel" aria-hidden="true">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header" style="display: inline">
                <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
                <h4 class="modal-title">批量修改参数单位</h4>
            </div>
            <div class="modal-body" style="word-break: break-all;">
                <div class="form-group">
                    <div class="form-inline" style="margin-bottom: 10px;">
                        <label class="col-3">协议类型：</label>

                        <div class="col-9">
                            <select id="batchTypeList" class="form-control" style="width: 95%"></select>
                        </div>
                    </div>

                    <div class="form-inline">
                        <label class="col-3">参数单位：</label>

                        <div class="col-9">
                            <input type="text" id="batchUpdateCodeDictUnit" maxlength="20" class="form-control" style="width: 95%"/>
                        </div>
                    </div>
                </div>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>
                <button type="button" class="btn btn-primary" onclick="_codeDictList.fn.batchUpdate()">确定</button>
            </div>
        </div>
        <!-- /.modal-content -->
    </div>
    <!-- /.modal-dialog -->
</div>

<!-- page script -->
<script type="text/javascript">
    let _codeDictList = {
        v: {
            list: [],
            dTable: null,
            ids: '',
            codeDictCodes: '',
            names: ''
        },
        fn: {
            init: function () {
                _codeDictList.fn.getTypeList();

                _codeDictList.fn.dataTableInit();
            },
            getTypeList: function () {
                let content = '<option value="">全部类型</option>';

                $.post('common/codeDictTypeList', {
                    id: 1
                }, function (result) {
                    if (result.length > 0) {
                        $.each(result, function (i, item) {
                            content += '<option value="' + item.msg + '">' + item.msg + '</option>';
                        });
                    }

                    $('#searchTypeList').append(content);
                    $('#typeList').append(content);
                    $('#batchTypeList').append(content);
                });
            },
            syncData: function () {
                $.post('backend/codeDict/sync', {
                    id: 1
                }, function (result) {
                    if (result.code === 0) {
                        info("信息已同步", "success");

                        _codeDictList.fn.reloadData();
                    } else {
                        info(result.msg, "error");
                    }
                });
            },
            reloadData: function () {
                _codeDictList.v.list = [];
                _codeDictList.v.dTable.DataTable().ajax.reload(null, false);
            },
            resetAndReloadData: function () {
                $('#searchTypeList').val('');
                $('#searchKeyWord').val('');

                _codeDictList.fn.reloadData();
            },
            dataTableInit: function () {
                _codeDictList.v.dTable = $tutu.dataTable($('#dataTables'), {
                    "processing": true,
                    "serverSide": true,
                    "autoWidth": true,//自动计算宽度
                    "lengthChange": false,//是否允许用户改变表格每页显示的记录数
                    "searching": false,
                    "ordering": false,
                    "ajax": {
                        "url": "backend/codeDict/list",
                        "type": "POST"
                    },
                    "language": $tutu.v.dataTableL,
                    "columns": [
                        {"data": "id"},
                        {"data": "type"},
                        {"data": "code"},
                        {"data": "name"},
                        {"data": "unit"},
                        {"data": null}
                    ],
                    /*添加小插件*/
                    "columnDefs": [
                        {
                            "data": null,
                            "defaultContent": "<button type='button' title='编辑' class='btn btn-primary btn-sm edit'><i class='fa fa-edit'></i></button>" +
                                "&nbsp;&nbsp;" +
                                "<button type='button' title='删除' class='btn btn-danger btn-sm delete'><i class='fa fa-remove'></i></button>",
                            "targets": -1
                        }
                    ],
                    /*展示数据格式化*/
                    "createdRow": function (row, data, index) {
                        _codeDictList.v.list.push(data);
                        $('td', row).eq(0).html("<input type='checkbox' name='checkboxId' value=" + data.id + ">");
                    },
                    /*回调函数*/
                    rowCallback: function (row, data) {
                        $('td', row).last().find(".edit").click(function () {
                            _codeDictList.fn.editInfo(data);
                        });

                        $('td', row).last().find(".delete").click(function () {
                            _codeDictList.fn.delInfo(data.id);
                        });
                    },
                    /*带参数*/
                    "fnServerParams": function (aoData) {
                        _codeDictList.v.list = [];
                        aoData.type = $('#searchTypeList').val();
                        aoData.keyWord = $('#searchKeyWord').val();
                    },
                    "fnDrawCallback": function (data) {
                        $tutu.uiForm(data);
                    }
                });
            },
            saveInfoPlus: function () {
                _codeDictList.fn.saveInfo(1);
            },
            saveInfo: function (type) {
                let name = $('#codeDictName').val();
                let code = $('#codeDictCode').val();

                if (name === '') {
                    info('请输入参数名称', "error");
                    return;
                }

                if (code === '') {
                    info('请输入参数编号', "error");
                    return;
                }

                startLoading();

                $.post('backend/codeDict/save', {
                    id: $('#codeDictId').val(),
                    type: $('#typeList').val(),
                    name: name,
                    code: code,
                    unit: $('#codeDictUnit').val()
                }, function (result) {
                    endLoading();

                    if (result.code === 0) {
                        info("信息已保存", "success");

                        if (type === 0) {
                            $("#codeDictModal").modal("hide");
                        } else {
                            $('#codeDictId').val('');
                        }

                        _codeDictList.fn.reloadData();
                    } else {
                        info(result.msg, "error");
                    }
                });
            },
            editInfo: function (data) {
                $('#codeDictId').val(data.id);
                $('#codeDictName').val(data.name);
                $('#codeDictCode').val(data.code);
                $('#codeDictUnit').val(data.unit);
                $('#typeList').val(data.type);

                $("#codeDictModal").modal("show");
            },
            addInfo: function () {
                $('#codeDictId').val('');
                $('#codeDictName').val('');
                $('#codeDictCode').val('');
                $('#codeDictUnit').val('');
                $('#typeList').val('');

                $("#codeDictModal").modal("show");
            },
            delInfo: function (id) {
                swal({
                    title: "您确定要删除这条记录吗",
                    text: "删除后将无法恢复，请谨慎操作！",
                    type: "warning",
                    showCancelButton: true,
                    confirmButtonColor: "#DD6B55",
                    confirmButtonText: "是的，删除！",
                    cancelButtonText: "不，取消",
                    closeOnConfirm: false,
                    closeOnCancel: false
                }, function (isConfirm) {
                    if (isConfirm) {
                        startLoading();

                        $.post('backend/codeDict/del', {
                            id: id
                        }, function (result) {
                            endLoading();

                            if (result.code === 0) {
                                swal("删除成功！", "您已经删除了这条记录。", "success");

                                _codeDictList.fn.reloadData();
                            } else {
                                info(result.msg, "error");
                            }
                        });
                    } else {
                        swal("已取消", "您取消了删除操作！", "error");
                    }
                })
            },
            batchUpdateModal: function () {
                let ids = "";

                if ($('input:checkbox[name=checkboxId]:checked').length === 0) {
                    info("请先选择至少一项", "error");
                } else {
                    $('input:checkbox[name=checkboxId]:checked').each(function (i) {
                        if (0 === i) {
                            ids = $(this).parent().next().next().text();
                        } else {
                            ids += ("," + $(this).parent().next().next().text());
                        }
                    });

                    _codeDictList.v.codeDictCodes = ids;

                    $('#updateModal').modal('show');
                }
            },
            batchDelModal: function () {
                let ids = "";
                let names = "";

                if ($('input:checkbox[name=checkboxId]:checked').length === 0) {
                    info("请先选择至少一项", "error");
                } else {
                    $('input:checkbox[name=checkboxId]:checked').each(function (i) {
                        if (0 === i) {
                            ids = $(this).val();
                            names = $(this).parent().next().next().html();
                        } else {
                            ids += ("," + $(this).val());
                            names += ("," + $(this).parent().next().next().html());
                        }
                    });

                    _codeDictList.v.ids = ids;
                    $('#delSpan').html(names);

                    $('#delModal').modal('show');
                }
            },
            batchDel: function () {
                startLoading();

                $.post('backend/codeDict/batchDel', {
                    ids: _codeDictList.v.ids
                }, function (result) {
                    endLoading();

                    if (result.code === 0) {
                        $('#delModal').modal('hide');
                        swal("删除成功！", "您已经删除了这些参数", "success");

                        setTimeout(function () {
                            _codeDictList.fn.reloadData();
                        }, 1000);
                    } else {
                        info(result.msg, "error");
                    }
                });
            },
            batchUpdate: function () {
                let selectType = $('#batchTypeList').val();
                let codeDictUnit = $('#batchUpdateCodeDictUnit').val();

                if (selectType === '') {
                    info('请选择参数类型', "error");
                    return;
                }

                if (codeDictUnit.trim().length === 0) {
                    info('参数单位不能为空', "error");
                    return;
                }

                startLoading();

                $.post('backend/codeDict/batchUpdate', {
                    type: selectType,
                    codeDictCodes: _codeDictList.v.codeDictCodes,
                    unit: codeDictUnit
                }, function (result) {
                    endLoading();

                    if (result.code === 0) {
                        $('#updateModal').modal('hide');

                        swal("修改成功！", "您已经修改了这些参数的单位", "success");

                        setTimeout(function () {
                            _codeDictList.fn.reloadData();
                        }, 1000);
                    } else {
                        info(result.msg, "error");
                    }
                });
            }
        }
    };

    $(document).ready(function () {
        _codeDictList.fn.init();

        window.onload = function () {
            checkSelectMenu("codeDictLi");

            loadMenuColor('codeDictLi');
        }
    });
</script>
</body>
</html>